/**
 * @class   wosh::security::AlarmEngine
 * @brief   The class provides basic functionalities for processing messages
 *          and evaluate triggers and events.
 *
 *
 ****************************************************************************
 * @version $Id: AlarmEngine.h 2574 2010-05-27 09:04:30Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_AlarmEngine_H__
 #define __WOSH_Services_AlarmEngine_H__

 #include <core/Thread.h>
 #include <core/Containers.h>
 #include <core/LogContext.h>
 #include <core/MessageQueue.h>

 #include <framework/security/AlarmState.h>
 #include <framework/security/AlarmTrigger.h>
 #include <framework/security/AlarmBrain.h>
 #include <framework/security/AlarmListeners.h>


namespace wosh {
 namespace security {


class AlarmEngine :	protected ThreadImpl,
					public virtual IAlarmBrainListener,
					public virtual IAlarmStateListener
 {
	WOSH_CLASSNAME(wosh::security::AlarmEngine)

	typedef ListT<wosh::security::AlarmTrigger*> tAlarmTriggerList;


	public:
		/**
		 * @brief   Default constructor. Init vars.
		 */
		AlarmEngine();
		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~AlarmEngine();


	public:
		virtual WRESULT arm( unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT disarm( unsigned long timeout = LIMIT_ULONG_MAX );


		virtual WRESULT evalMessage( const Message& message );

		virtual WRESULT addTrigger( AlarmTrigger* trigger );

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		void setAlarmListener(IAlarmEngineListener* listenerPtr )  { this->listener = listenerPtr; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:

		AlarmState* getAlarmState()							{ return this->alarmState; }

		AlarmBrain* getAlarmBrain()							{ return this->alarmBrain; }

		const tAlarmTriggerList& getAlarmTriggers() const	{ return this->alarmTriggers; }
		tAlarmTriggerList& getAlarmTriggers()				{ return this->alarmTriggers; }

		IAlarmEngineListener* getAlarmListener()			{ return this->listener; }

//@}
/** @name IAlarmBrainListener Interface
 * @{
 ******************************************************************************/
 	public:
		virtual void raised_AlarmBrain_Error( int error, const std::string& description, AlarmBrain* source );

//@}
/** @name IAlarmStateListener Interface
 * @{
 ******************************************************************************/
 	public:
		virtual void changed_AlarmState( int state, AlarmState* source );
		virtual void raised_AlarmEvent( const AlarmEvent* alarm_event, AlarmState* source );

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		/**
		 * @brief   
		 * @see     running
		 * @see     
		 */
		virtual void runThread();

//@}
	protected:

		MessageQueue msgQueue;				///< messages' queue
		wosh::LogContext Log;				///< my log

		AlarmState* alarmState;				///< current state of alarm system

		AlarmBrain* alarmBrain;				///< the "brain" which evaluates previous and current triggers

		tAlarmTriggerList alarmTriggers;	///< triggers which may update current state


		IAlarmEngineListener* listener;		///< callbacks


}; // class def

 }; // namespace security
}; // namespace wosh

#endif //__WOSH_Services_AlarmEngine_H__
